package com.annie.piglatin;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener{
	
	public static final String PIGLATIN_KEY = "words";
	//where the text is entered
    private EditText toTranslate;
    private String pigLatin;

    
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //setting the views
        toTranslate = (EditText) findViewById(R.id.toTranslate);
    }
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    // Inflate the menu items for use in the action bar
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu, menu);
	    return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle presses on the action bar items
		
		InputMethodManager imm = (InputMethodManager)getSystemService(
			      Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(toTranslate.getWindowToken(), 0);
			
	    switch (item.getItemId()) {
	        case R.id.action_submit:
	        	String text = toTranslate.getText().toString();
			try {
				pigLatin = PigLatinConverter.convertToPigLatin(text);
			} catch (StringFormatException e) {
				pigLatin = e.getMessage();
				e.printStackTrace();
			}
	    		
	    		MyDialogFragment myDialogFragment = new MyDialogFragment();
	            Bundle args = new Bundle();
	            args.putString(PIGLATIN_KEY, pigLatin);
	            myDialogFragment.setArguments(args);
	            myDialogFragment.show(getFragmentManager(), "tag");
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}

	@Override
	public void onClick(View arg0) {
		// takes the text and sends it to be converted then puts it into a dialog box
		
	}
    
	public void resetText (){
		toTranslate.setText("");
	}
}
